//
//  LeftViewController.m
//  KYProject
//
//  Created by Roman on 16/12/20.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "LeftViewController.h"
#import "DoctorApplyController.h"
#import "CaseManageViewController.h"
#import "FocusDoctorController.h"
#import "SettingViewController.h"
#import "AboutProductViewController.h"
#import "SharedViewController.h"
#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UserManager.h"





@interface LeftViewController ()<UITableViewDelegate,UITableViewDataSource>
/**有登陆界面*/
@property (nonatomic,strong)UIView *loginBGView;

/** 没有登录界面 */
@property (nonatomic, strong) UIView *noLoginBGView;

/**tableView*/
@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSArray *arrData1;
@property (nonatomic, strong) NSArray *arrData2;

@property (nonatomic, strong) NSArray *viewControllers;
//记录用户登录成功后传回来的数据
@property (nonatomic,strong)Users *us;

//头像
@property (nonatomic,weak)UIImageView *iconV;

//姓名
@property (nonatomic,weak)UILabel *nameLabel;

//遮罩
//@property (nonatomic, strong) NSArray *viewControllers;


@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureUI];
    [self setupUI];
    //订阅轻触主界面vc通知
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeVcTap) name:@"centerVcTap" object:nil];
    // Do any additional setup after loading the view.
}
//视图将要出现,沙盒判断当前是否登录
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self configureLoginView];
}

- (void)configureLoginView
{

    BOOL isLogin = [(NSUserDefaults *)[NSUserDefaults standardUserDefaults] boolForKey:kIsUserLoginKey];
//    
    self.noLoginBGView.hidden = isLogin;
    
    self.loginBGView.hidden = !isLogin;
    
    self.tableview.hidden = !isLogin;


    _isLogin = isLogin;
}

//存储用户的登录数据
- (void)setUs:(Users *)us
{
    _us = us;
    self.iconV.image = [UIImage imageNamed:@"banner"];
    self.nameLabel.text = self.us.true_name;
    //路径-->用户信息归档
    [UserManager sharedManager].userInfo = us;
    
    #pragma mark - 存储修改用户信息界面的假数据到沙盒中
    
}



#pragma mark - 配置
- (void)configureUI
{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = YES;
    [self.tableview registerClass:[UITableViewCell class ] forCellReuseIdentifier:@"cell"];
    self.arrData1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.arrData2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];


  
    
    self.viewControllers = @[@"DoctorApplyController", @"CaseManageViewController",
                             @"FocusDoctorController", @"SettingViewController",
                             @"AboutProductViewController", @"SharedViewController"];
    

}
#pragma mark - 设置UI
- (void)setupUI
{
    
    [self.view addSubview:self.loginBGView];
    
    [self.view addSubview:self.noLoginBGView];
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.loginBGView.mas_bottom);
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width*0.8);
        
        //取消分割线
        self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    }];
   
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 0) {
        cell.textLabel.text = self.arrData1[indexPath.row];
    }
    else
    {
        cell.textLabel.text = self.arrData2[indexPath.row];
    }
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.01;
    }
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    //创建一个空的,下面重新赋值
    UIViewController *newVC = nil;
    if (section == 0) {
        switch (row) {
            case 0:
                newVC = (DoctorApplyController *)[[DoctorApplyController alloc]init];
                
                break;
                
                case 1:
                
                newVC = (CaseManageViewController *)[[CaseManageViewController alloc]init];
                
                break;
                case 2:
                newVC = (FocusDoctorController *)[[FocusDoctorController alloc]init];
                break;
            default:
                break;
        }
        //都要设置所在控制器title值
        newVC.title = self.arrData1[row];
    }
    else
    {
        switch (row) {
            case 0:
                
            {
                SettingViewController *set =[SettingViewController new];
                
                set.cancellationBlock =^(){
                    //注销用户
                    [[UserManager sharedManager]cancellationUserInfo];
                    [self configureLoginView];
                };
                newVC = set;
            }
                break;
                
            case 1:
                
                newVC = (AboutProductViewController *)[[AboutProductViewController alloc] init];
                break;
                
            case 2:
                
                newVC = (SharedViewController *)[[SharedViewController alloc] init];
                //框架分享功能
                //1.创建分享参数
                break;
            default:
                break;
        }
        
        
        newVC.title = self.arrData2[row];

    }
    [self jumpToOtherViewController:newVC];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Actions
-(void)clickButton:(UIButton *)btn
{
    //点击登陆
    
    if (btn.tag == 101) {
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        __weak LeftViewController *selfVC = self;
        loginVC.LogInCompleteBlock = ^(Users *us)
        {
            [selfVC configureLoginView];
            selfVC.us = us;
        };
        [self jumpToOtherViewController:loginVC];
    }
    //点击注册
    else
    {
        RegisterViewController *registerVC = [[RegisterViewController alloc]init];
        registerVC.LogInCompleteBlock = ^(Users *us)
        {
            NSLog(@"晕%@",us);
            [self configureLoginView];
            self.us = us;
        };
        [self jumpToOtherViewController:registerVC];
    }
}


#pragma mark - Lazy
- (UITableView *)tableview
{
    if (_tableview == nil) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        _tableview.dataSource = self;
        _tableview.delegate = self;
    }
    return _tableview;
}


-(UIView *)noLoginBGView{
    
    if (_noLoginBGView == nil) {
        _noLoginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width , 120)];
        
//        UIImage *backImg = [UIImage imageNamed:@"new_background"];
//        UIColor *bc = [UIColor colorWithPatternImage:backImg];
//        _noLoginBGView.backgroundColor = bc;
        _noLoginBGView.backgroundColor = [UIColor blueColor];
        
        //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        
        iconV.image = [UIImage imageNamed:@"name"];
        [_noLoginBGView addSubview:iconV];
        iconV.bounds = CGRectMake(0, 0, 45, 45);
        iconV.center = CGPointMake([UIScreen mainScreen].bounds.size.width*0.8/2, 50);
        
        
        //登录
        UIButton *leftButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:leftButton];
        leftButton.tag = 101;
        [leftButton setTitle:@"登录" forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.right.mas_equalTo(iconV.mas_centerX).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        //注册
        UIButton *rightButton = [[UIButton alloc] init];
        [_noLoginBGView addSubview:rightButton];
        rightButton.tag = 102;
        [rightButton setTitle:@"注册" forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).offset(10);
            make.left.mas_equalTo(iconV.mas_centerX).offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
    }
    
    return _noLoginBGView;
}

-(UIView *)loginBGView{
    
    if (_loginBGView == nil) {
        _loginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width , 120)];
    
        _loginBGView.backgroundColor = [UIColor grayColor];
        
      
        [[UserManager sharedManager]getUserInfoFromCache];
        Users *us = [UserManager sharedManager].userInfo;
               ;
       //头像
        UIImageView *iconV = [[UIImageView alloc] init];
        self.iconV = iconV;
                if (us) {
                    [iconV sd_setImageWithURL:[NSURL URLWithString:us.head_photo]];
                }else{
                    iconV.image = [UIImage imageNamed:@"h3"];
                }
        //给头像添加手势tap
        iconV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickIconV:)];
        [iconV addGestureRecognizer:tap];
        
        iconV.image = [UIImage imageNamed:@"h3"];
        [_loginBGView addSubview:iconV];
        iconV.layer.cornerRadius = 35;
        iconV.layer.masksToBounds = YES;
        
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.left.mas_equalTo(20);
            make.top.mas_equalTo(30);
        }];
        
        //昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        self.nameLabel = nameLabel;
        if (us) {
            nameLabel.text = us.true_name;
        }else{
            nameLabel.text = @"王二";
        }
        nameLabel.font = [UIFont systemFontOfSize:16];
        

        [_loginBGView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(iconV).offset(5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
            
        }];
        
        //个人资料
        UILabel *descLabel = [[UILabel alloc] init];
        descLabel.text = @"查看个人资料";
        nameLabel.font = [UIFont systemFontOfSize:16];
        [_loginBGView addSubview:descLabel];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(iconV).offset(-5);
            make.left.mas_equalTo(iconV.mas_right).offset(15);
        }];
    }
    
    return _loginBGView;
}

//头像的点击监听
- (void)clickIconV:(UITapGestureRecognizer *)tap{
    
    NSLog(@"点击了头像");
  
    
}

//存储用户的登录数据





//- (UIView *)loginBGView
//{
//    
//    if (_loginBGView == nil) {
//        _loginBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 120)];
//        _loginBGView.backgroundColor = [UIColor yellowColor];
//        
//        //头像
//        UIImageView *iconV = [[UIImageView alloc]init];
//        iconV.image = [UIImage imageNamed:@"banner"];
//        [_loginBGView addSubview:iconV];
//        iconV.layer.cornerRadius = 35;
//        iconV.layer.masksToBounds = YES;
//        
//        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(70, 70));
//            make.left.mas_equalTo(20);
//            make.top.mas_equalTo(30);
//        }];
//        
//        //昵称
//        UILabel *nameLabel = [[UILabel alloc]init];
//        nameLabel.text = @"周杰伦";
//        nameLabel.font = [UIFont systemFontOfSize:16];
//        [_loginBGView addSubview:nameLabel];
//        
//        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            
//            make.top.equalTo(iconV).offset(5);
//            make.left.mas_equalTo(iconV.mas_right).offset(15);
//        }];
//        
//        //个人资料
//        UILabel *descLabel = [[UILabel alloc]init];
//        descLabel.text = @"查看个人资料";
//        descLabel.font = [UIFont systemFontOfSize:16];
//        [_loginBGView addSubview:descLabel];
//        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            
//            make.bottom.equalTo(iconV).offset(-5);
//            make.left.mas_equalTo(iconV.mas_right).offset(15);
//        }];
//    }
//    return _loginBGView;
//}


@end
